package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.DefaultRowSorter;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.RowSorter;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

public class PfShow_all_recipes_that_can_buy  extends JPanel{

	private Vector<Vector> menu;            // all recipes that can buy in restorant  (Rid,Rname,Rweight,price)           
	private MainMenu mainMenu;
	private DefaultTableModel dtm;
	private JTable tbl;
	private RowSorter<DefaultTableModel> sorterName;
	private RowSorter<DefaultTableModel> sorterPriceRecipe;
	private JTextField priceRecipeTF;
	private JTextField nameTF;
	
	public PfShow_all_recipes_that_can_buy(MainMenu mm, Vector<Vector> menuGen)
	{
		super();
		this.setLayout(new BorderLayout());
		this.mainMenu=mm;
		this.menu=menuGen;                        
		initiliaze();
		System.out.println(menu);
	
	}
	
	
	
	private void initiliaze()
	{
		JScrollPane scrollPane = new JScrollPane(getTblMenu());
		this.add(scrollPane,BorderLayout.CENTER);
		this.add(getSearchUnits(),BorderLayout.NORTH);
	}
	
	
	

	private JTable getTblMenu()                                  //Method return  ready table with all recipes from "recipes_in_menu"
	{
		Vector <String> tblheader=new Vector<String>();
		tblheader.add("מס' מתכון");
		tblheader.add("שם מתכון");
		tblheader.add("משקל מתכון");
		tblheader.add("מחיר מתכון");
		
		
		
		dtm=new DefaultTableModel(menu,tblheader);	 		
		tbl = new JTable(dtm);
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		for(int x=0;x<4;x++){
			tbl.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
	        }
		tbl.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		tbl.setFont(new Font("David", Font.BOLD, 16));
		
			  
		
		tbl.getTableHeader().setFont( new Font( "Arial" , Font.BOLD, 17 ));
		tbl.getTableHeader().setBackground(new Color(173,216,230));
		tbl.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
	    tbl.setRowHeight(30);
	
		tbl.setFillsViewportHeight(true);
        tbl.setGridColor(new Color(25, 25, 112));
        tbl.setBackground(new Color(250, 235, 215));
        tbl.setForeground(new Color(0, 0, 0));
        tbl.setAutoCreateRowSorter(true);
        
        
        
        tbl.addMouseListener(new MouseAdapter() {
			   public void mouseClicked(MouseEvent e) {
			     if (e.getClickCount() == 1) {
			       JTable target = (JTable)e.getSource();
			       int row = target.getSelectedRow();
			       int column = target.getSelectedColumn();
			       // do some action if appropriate column
			       mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
			       int row_index=target.getSelectedRow();
			       Vector<String> vec=(Vector<String>) dtm.getDataVector().get(row_index);
			       mainMenu.getContentPane().add(new PanelFunctionListOfAllRecipes(mainMenu,vec), BorderLayout.EAST);
			       mainMenu.getContentPane().validate();
			     }
			   }
			 });
        
        
        return tbl;
	}
	
	private JPanel getSearchUnits()                 //Panel with textfields for a fast search
	{
		JPanel searchUnits=new JPanel ();
		searchUnits.setBackground(new Color(255, 245, 238));
		searchUnits.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		searchUnits.add(getNameTF());
		searchUnits.add(getNameLbl());
		searchUnits.add(getPriceRecipeTF());
		searchUnits.add(getPriceRecipeLbl());
		//searchUnits.add(getRolesCB());
		//searchUnits.add(getRoleLbl());
		return searchUnits;
	}
	
	private JTextField getNameTF()
	{
		nameTF=new JTextField(20);
		nameTF.setHorizontalAlignment(JTextField.RIGHT);
	
		KeyListener keyListener = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg0) {}

			public void keyReleased(KeyEvent arg0) 
			{
				JTextField nameField = (JTextField)arg0.getSource();
				String selectedName = (String)nameField.getText();
				filterByName(selectedName);				
			}

			public void keyTyped(KeyEvent arg0) {}		
		};
		
		nameTF.addKeyListener(keyListener);		
		return nameTF;
	}
	
	private void filterByName(String name)
	{
		
		sorterName = new TableRowSorter<DefaultTableModel>(dtm);     		   
	    tbl.setRowSorter(sorterName);  	    
	    RowFilter< DefaultTableModel  , Object> rf = null;   
	    //declare a row filter for your table model  
	    try  
	    {  
	        rf = RowFilter.regexFilter(name, 1);    
	        //initialize with a regular expression  
	    }  
	   catch (java.util.regex.PatternSyntaxException e1)  
	    {  
	        return;  
	    }  
	   
	    ((DefaultRowSorter<DefaultTableModel, Integer>) sorterName).setRowFilter(rf);		
	}
	
	
	private JTextField getPriceRecipeTF()
	{
	    priceRecipeTF=new JTextField(20);
	    priceRecipeTF.setHorizontalAlignment(JTextField.RIGHT);
	    
	    KeyListener keyListener = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg0) {}

			public void keyReleased(KeyEvent arg0) 
			{
				// TODO Auto-generated method stub
				JTextField priceField = (JTextField)arg0.getSource();
				String selectedPriceMenu = (String)priceField.getText();
				filterByPriceRecipe(selectedPriceMenu);				
			}

			public void keyTyped(KeyEvent arg0) {}		
		};
		
		priceRecipeTF.addKeyListener(keyListener);		
		
		return priceRecipeTF;
	}
	
	private void filterByPriceRecipe(String priceMenu)
	{
		
		sorterPriceRecipe = new TableRowSorter<DefaultTableModel>(dtm);     		   
	    tbl.setRowSorter(sorterPriceRecipe);  	    
	    RowFilter< DefaultTableModel  , Object> rf = null;   
	    //declare a row filter for your table model  
	    try  
	    {  
	        rf = RowFilter.regexFilter(priceMenu, 2);    
	        //initialize with a regular expression  
	    }  
	   catch (java.util.regex.PatternSyntaxException e1)  
	    {  
	        return;  
	    }  
	   
	    ((DefaultRowSorter<DefaultTableModel, Integer>) sorterPriceRecipe).setRowFilter(rf);		
	}
	
	
	private JLabel getNameLbl()
	{
		JLabel nameLbl=new JLabel("  שם :  ");
		nameLbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		return nameLbl;
	}
	
	private JLabel getPriceRecipeLbl()
	{
		JLabel priceMenuLbl=new JLabel(" מחיר  :  ");
		priceMenuLbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		return priceMenuLbl;
	}
	
	
}


